﻿using LoungeRepo.Contracts.Core;
using LoungeRepo.Contracts.Normalizer;
using LoungeRepo.Normalizer.DataStructures;

namespace LoungeRepo.Normalizer
{
    partial class ObjectGraphNormalizer
    {
        private NormalizedEntity CreateNormalizedEntityToBeFilled(object entityObject, ILoungeRepoEntityIdentity entityIdentity)
        {
            this.reportEntity(entityObject, entityIdentity);

            var normalizedEntity = new NormalizedEntity(entityIdentity.Id, entityIdentity.Partition, entityObject.GetType());
            RegisterNormalizedEntityForDelivery(normalizedEntity);
            return normalizedEntity;
        }

        private NormalizedEntityReference CreateReferenceToNormalizedEntity(object entityObject, ILoungeRepoEntityIdentity entityIdentity)
        {
            var normalizedEntityRef = new NormalizedEntityReference(entityIdentity);
            RegisterNormalizedEntityForFutureReferences(entityObject, normalizedEntityRef);
            return normalizedEntityRef;
        }

        private NormalizedObject CreateNormalizedObjectToBeFilled(object nonEntityObject)
        {
            NormalizedObject normalizedObject = new NormalizedObject(nonEntityObject.GetType());
            RegisterNormalizedObjectForFutureReferences(nonEntityObject, normalizedObject);
            return normalizedObject;
        }


        private void RegisterNormalizedEntityForDelivery(INormalizedEntity normalizedEntity)
        {
            this.normalizedEntities.Add(normalizedEntity);
        }

        private void RegisterNormalizedEntityForFutureReferences(object entityObject, INormalizedObjectReference normalizedEntityRef)
        {
            this.normalizedObjectCache.Add(entityObject, normalizedEntityRef);
        }

        private void RegisterNormalizedObjectForFutureReferences(object nonEntityObject, INormalizedObject normalizedObject)
        {
            this.normalizedObjectCache.Add(nonEntityObject, new NormalizedObjectReference(normalizedObject));
        }
    }
}
